package org.viva.service.cms;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;
import org.viva.admin.util.page.Page;
import org.viva.core.dao.BaseDAO;
import org.viva.entity.cms.Movie;

@Service("movieService")
public class MovieService {

    @Resource
    private BaseDAO<Movie> dao;

    @SuppressWarnings("unchecked")
    public List<Movie> getList(Map<String, Object> param) {
        Criteria criteria = dao.getCurrentSession().createCriteria(Movie.class);
        criteria.add(Restrictions.eq("cgId", Long.parseLong(param.get("cgid") + "")));
        return criteria.list();
    }

    public Movie get(Long id) {
        return dao.get(Movie.class, id);
    }

    public Page getPage(Map<String, Object> param) {
        Page page = new Page(param);
        Criteria criteria = dao.getCurrentSession().createCriteria(Movie.class);
        if (param.get("cgid") != null) {
            criteria.add(Restrictions.eq("cgId", Long.parseLong(param.get("cgid") + "")));
        }
        if (param.get("title") != null && !"".equals(param.get("title") + "")) {
            criteria.add(Restrictions.like("title", param.get("title") + "", MatchMode.ANYWHERE));
        }
        if (param.get("tag") != null && !"".equals(param.get("tag") + "")) {
            criteria.add(Restrictions.like("tags", param.get("tag") + "", MatchMode.ANYWHERE));
        }
        if (param.get("year") != null && !"".equals(param.get("year") + "")) {
            criteria.add(Restrictions.eq("year", param.get("year") + ""));
        }
        if (param.get("area") != null && !"".equals(param.get("area") + "")) {
            criteria.add(Restrictions.like("area", param.get("area") + "", MatchMode.ANYWHERE));
        }
        // 获取根据条件分页查询的总行数
        int rowCount = (int) ((long) criteria.setProjection(Projections.rowCount()).uniqueResult());

        if (param.get("ord") != null && !"".equals(param.get("ord") + "")) {
            if ("desc".equals(param.get("ordtype") + "")) {
                criteria.addOrder(Order.desc(param.get("ord") + ""));
            } else {
                criteria.addOrder(Order.asc(param.get("ord") + ""));
            }
        }
        criteria.addOrder(Order.desc("id"));
        criteria.setProjection(null);
        criteria.setFirstResult((page.getPage() - 1) * page.getRows());
        criteria.setMaxResults(page.getRows());
        page.addDate(criteria.list(), rowCount);
        return page;
    }

    public List<Movie> pic(boolean isNull) {
        Criteria criteria = dao.getCurrentSession().createCriteria(Movie.class);
        if (isNull) criteria.add(Restrictions.isNull("img"));
        else criteria.add(Restrictions.isNotNull("img"));
        criteria.addOrder(Order.desc("year"));
        criteria.addOrder(Order.desc("id"));
        criteria.setProjection(null);
        criteria.setMaxResults(500);
        return criteria.list();
    }

    public void up(Movie movie) {
        Movie c = dao.get(Movie.class, movie.getId());
        c.setTitle(movie.getTitle());
        c.setContent(movie.getContent());
        c.setCreateDate(movie.getCreateDate());
        c.setTags(movie.getTags());
        c.setCgId(movie.getCgId());
        c.setUserId(movie.getUserId());
        initMovie(c);
    }

    public void add(Movie movie) {
        movie.setCommCount(0L);
        movie.setViewCount(0L);
        initMovie(movie);
        dao.saveOrUpdate(movie);
    }

    private void initMovie(Movie c) {
        try {
            Document doc = Jsoup.parse(c.getContent());
            {
                String A_INTRO = doc.text();
                // 简介
                A_INTRO = A_INTRO.replaceAll("[\r\n]", "");
                A_INTRO = A_INTRO.substring(0, A_INTRO.length() > 300 ? 300 : A_INTRO.length());
                c.setIntro(A_INTRO);
            }
            {
                Elements elements = doc.select("img");
                if (elements.size() > 0) {
                    String img = elements.get(0).attr("src");
                    c.setImg(img);
                }
            }
        } catch (Exception e) {
        }
    }

}
